/// <reference path="../Bootstrap/js/bootstrap.min.js" />
/// <reference path="../Knockout/knockout-3.3.0.js" />
/// <reference path="mars.validation.js" />
/// <reference path="mars.constants.js" />
/// <reference path="jquery-1.11.2.min.js" />
/// <reference path="mars.min.js" />
/// <reference path="mars.masterlists.js" />
/// <reference path="../JQueryValidation/jquery.validate.min.js" />

var DocUtility = {
    CreateMarsFolders: function (validationId) {
        if (validationId != undefined && validationId != "" && validationId != undefined) {
            DocUtility.AddFolder(MARS_CONSTANTS.MARS_LIST_NAMES.MARS_DOCUMENTS, validationId);
            DocUtility.AddFolder(MARS_CONSTANTS.MARS_LIST_NAMES.MARS_DOCUMENTS + "/" + validationId, MARS_CONSTANTS.FOLDER_NAMES.DEVELOPMENT_DOCUMENTS);
            DocUtility.AddFolder(MARS_CONSTANTS.MARS_LIST_NAMES.MARS_DOCUMENTS + "/" + validationId, MARS_CONSTANTS.FOLDER_NAMES.IMPLEMENTATION_DOCUMENTS);
            DocUtility.AddFolder(MARS_CONSTANTS.MARS_LIST_NAMES.MARS_DOCUMENTS + "/" + validationId, MARS_CONSTANTS.FOLDER_NAMES.INDEPENDENT_VALIDATION_DOCUMENTS);
            DocUtility.AddFolder(MARS_CONSTANTS.MARS_LIST_NAMES.MARS_DOCUMENTS + "/" + validationId, MARS_CONSTANTS.FOLDER_NAMES.EXECUTIVE_SUMMARY);
            DocUtility.AddFolder(MARS_CONSTANTS.MARS_LIST_NAMES.MARS_DOCUMENTS + "/" + validationId, MARS_CONSTANTS.FOLDER_NAMES.DATA_SET);
            DocUtility.AddFolder(MARS_CONSTANTS.MARS_LIST_NAMES.MARS_DOCUMENTS + "/" + validationId, MARS_CONSTANTS.FOLDER_NAMES.RISK_DOCUMENTS);
            DocUtility.AddFolder(MARS_CONSTANTS.MARS_LIST_NAMES.MARS_DOCUMENTS + "/" + validationId, MARS_CONSTANTS.FOLDER_NAMES.ANCILLARY_DOCUMENTS);
        } else {
            alert("Error: Could not create folder structure for Model Documents.");
        }
    },

    AddFolder: function (relativeUrl, folderName) {
        var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/Web/GetFolderByServerRelativeUrl('" + relativeUrl + "')/Folders";
        var folderCall = $.ajax({
            url: requestUri,
            async: false,
            type: "POST",
            data: JSON.stringify({ '__metadata': { 'type': 'SP.Folder' }, 'ServerRelativeUrl': folderName }),
            headers: {
                "accept": "application/json;odata=verbose",
                "content-type": "application/json;odata=verbose",
                "X-RequestDigest": $("#__REQUESTDIGEST").val()
            }
        });
        folderCall.done(function (data, textStatus, jqXHR) {
            console.log("Created folder " + folderName);
        });
        folderCall.fail(function (error, y, errorText) {
            alert("Error: " + errorText);
            console.log(JSON.stringify(error));
        });
    },

    UploadFile: function (dialogTitle, folderPath, callbackFunction) {
        var options = {
            title: dialogTitle,
            showClose: true,
            autosize: true,
            url: "/_layouts/15/AIG.MARS.Forms/myupload.aspx?List={6E2E3EF5-CB42-49E0-A12E-F0BAF85C933B}&RootFolder=" + folderPath,
            dialogReturnValueCallback: callbackFunction
        };
        SP.UI.ModalDialog.showModalDialog(options);
    },

    GetFilesUnderFolder: function (relativeUrl) {
        var filesList = new Array();
        var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/Web/GetFolderByServerRelativeUrl('" + relativeUrl + "')/Files?$select=Name,ServerRelativeUrl";
        var folderCall = $.ajax({
            url: requestUri,
            async: false,
            type: "GET",
            headers: {
                "accept": "application/json;odata=verbose",
                "content-type": "application/json;odata=verbose",
            }
        });
        folderCall.done(function (data, textStatus, jqXHR) {
            var files = data.d.results;
            for (var i = 0; i < files.length; i++) {
                var File = {
                    Name: files[i].Name,
                    Url: _spPageContextInfo.webAbsoluteUrl + files[i].ServerRelativeUrl,
                    ServerRelativeUrl: files[i].ServerRelativeUrl,
                };
                filesList.push(File);
            }
        });
        folderCall.fail(function (error, y, errorText) {
            alert("Error: " + errorText);
            console.log(JSON.stringify(error));
        });
        return filesList;
    },

    DeleteDocument: function (relativeUrl) {
        var success = false;
        var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/Web/GetFileByServerRelativeUrl('" + relativeUrl + "')/";
        var deleteDocumentCall = $.ajax({
            url: requestUri,
            async: false,
            type: "POST",
            headers: {
                "IF-MATCH": "*",
                "accept": "application/json;odata=verbose",
                "content-type": "application/json;odata=verbose",
                "X-HTTP-Method": "DELETE",                
                "X-RequestDigest": $("#__REQUESTDIGEST").val()
            }
        });
        deleteDocumentCall.done(function (data, textStatus, jqXHR) {
            console.log("Deleted File: " + relativeUrl);
            success = true;
        });
        deleteDocumentCall.fail(function (error, y, errorText) {
            success = false;            
            console.log(JSON.stringify(error));
        });
        return success;
    },
}

